package com.ds.infra.rpc.example.consumer.controller;

import com.ds.infra.rpc.example.api.OrderService;
import com.ds.infra.rpc.example.api.dto.OrderDTO;
import com.ds.infra.rpc.example.api.exception.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

@RestController
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/hi")
    public String hi() {
        return "Order::" + ThreadLocalRandom.current().nextInt(10000);
    }

    @PostMapping
    public OrderDTO create(@RequestBody OrderDTO orderDTO) throws ApiException {
        return orderService.createOrder(orderDTO);
    }

    @GetMapping("/{id}")
    public OrderDTO getById(@PathVariable Long id) throws ApiException {
        return orderService.getOrderById(id);
    }

    @DeleteMapping("/{id}")
    public Boolean cancel(@PathVariable Long id) throws ApiException {
        return orderService.cancelOrder(id);
    }

    @GetMapping("/user/{userId}")
    public List<OrderDTO> getByUser(@PathVariable Long userId) throws ApiException {
        return orderService.getOrdersByUserId(userId);
    }
}
